package com.xinrui.sunnyday.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.xinrui.sunnyday.constant.MessageConstant;
import com.xinrui.sunnyday.constant.RedisMessageConstant;
import com.xinrui.sunnyday.entity.Result;
import com.xinrui.sunnyday.pojo.Order;
import com.xinrui.sunnyday.service.OrderService;
import com.xinrui.sunnyday.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/orderMobile")
public class OrderMobileController {
    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 用户预约
     *
     * @param orderInfo
     * @return
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody Map<String, Object> orderInfo) {
//        判断验证码是否正确
//        获取电话号码
        Object telephone = orderInfo.get("telephone");
//        将电话号码拼接
        String key = RedisMessageConstant.SENDTYPE_ORDER + "_" + telephone;
//        创建jedis对象
        Jedis resource = jedisPool.getResource();
        //        查询redis
        String s = resource.get(key);
        if (StringUtils.isEmpty(s)) {
//           等于空则验证码没有
            return new Result(false, "请重新发送验证码");
        }
//        前端传递过来的代码
        Object validateCode = orderInfo.get("validateCode");
//        判断是否一致
        if (!s.equals(validateCode)) {
//            不一致，弹出提示
            return new Result(false, "您输入的验证码有误");
        }
//        接收前台传过来的体检人信息,传递到service层
        Order order = orderService.submit(orderInfo);
        System.out.println(order);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order);
    }

    /**
     * 多表查询：查询订单信息
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        Map<String,Object> map=orderService.findById(id);
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
    }
}
